package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.HomePagePresenter
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.HomePageView
import com.weetop.cfw.bean.*
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.home_page.fragment.HomePageFragment
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class HomePagePresenterImp(var homePageView: HomePageView? = null) : HomePagePresenter {

    /**
     * 根据城市关键词获取分站信息
     */
    override fun getCityIdByCityName(homePageFragment: HomePageFragment, cityName: String) {
        RxJavaUtils.useInSupportFragmentOnDestoryView(RetrofitUtils.getApi().getCityIdByCityName(
            cityName
        ), homePageFragment,
            object : RxJavaCallBack<CityIdBean>() {
                override fun onSuccess(t: CityIdBean?) {
                    homePageView?.getCityIdSuccess(t)
                }
            })
    }

    /**
     * 获取用户信息
     */
    override fun getPersonalInfoData(homePageFragment: HomePageFragment) {
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().getPersonalInfo(),
            homePageFragment,
            object :
                RxJavaCallBack<PersonalInfoBean>() {
                override fun onSuccess(t: PersonalInfoBean?) {
                    homePageView?.personalInfoGetSuccess(t)
                }

                override fun onRequestError(t: PersonalInfoBean?) {
                    super.onRequestError(t)
                    homePageView?.showNativeShortToast(t?.message)
                    homePageFragment.dismissLoadingDialog()
                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)
                    homePageFragment.dismissLoadingDialog()
                }
            })
    }

    /**
     * 获取上市服务富文本数据
     */
    override fun getListingServiceData(homePageFragment: HomePageFragment) {
        /*homePageFragment.showLoadingDialog(
            homePageFragment.activity as CommonBaseActivity,
            "正在获取服务数据..."
        )*/
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().getAppRelativeSinglePageIntroduce(
                "appstock"
            ), homePageFragment, object : RxJavaCallBack<SinglePageIntroduceBean>() {
                override fun onSuccess(t: SinglePageIntroduceBean?) {
                    homePageFragment.dismissLoadingDialog()
                    homePageView?.listingServiceDataGetSuccess(t)
                }

                override fun onRequestError(t: SinglePageIntroduceBean?) {
                    super.onRequestError(t)
                    homePageFragment.dismissLoadingDialog()
                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)
                    homePageFragment.dismissLoadingDialog()
                }
            })
    }

    /**
     * 获取上市服务富文本数据
     */
    override fun getIndustrialAndCommercialTaxationData(homePageFragment: HomePageFragment) {
        /*homePageFragment.showLoadingDialog(
            homePageFragment.activity as CommonBaseActivity,
            "正在获取服务数据..."
        )*/
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().getAppRelativeSinglePageIntroduce(
                "apptax"
            ), homePageFragment, object : RxJavaCallBack<SinglePageIntroduceBean>() {
                override fun onSuccess(t: SinglePageIntroduceBean?) {
                    homePageFragment.dismissLoadingDialog()
                    homePageView?.industrialAndCommercialTaxationDataFetSuccess(t)
                }

                override fun onRequestError(t: SinglePageIntroduceBean?) {
                    super.onRequestError(t)
                    homePageFragment.dismissLoadingDialog()
                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)

                    homePageFragment.dismissLoadingDialog()
                }
            })
    }

    /**
     * 获取首页Banner图数据
     */
    override fun getHomePageBannerData(homePageFragment: HomePageFragment, currentPage: Int) {
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().getAdvertisementData(
                adpos = 19,
                curpage = currentPage
            ), homePageFragment, object : RxJavaCallBack<AdvertisementBean>() {
                override fun onSuccess(t: AdvertisementBean?) {
                    homePageView?.advertisementBannerDataGetSuccess(t)
                }
            })
    }

    /**
     * 获取首页Banner图数据 修改
     */
    override fun getHomePageBannerExData(
        homePageFragment: HomePageFragment,
        currentPage: Int
    ) {
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().getAdvertisementExData(adpos = 19, curpage = currentPage),
            homePageFragment,
            object : RxJavaCallBack<AdvertisementBean>() {
                override fun onSuccess(t: AdvertisementBean?) {
                    homePageView?.advertisementBannerDataGetSuccess(t)
                }
            })
    }

    /**
     * 获取首页活动推荐
     */
    override fun getRecommendActivitiesList(homePageFragment: HomePageFragment, currentPage: Int) {
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().getRecommendActivitiesList(
                adpos = 20,
                curpage = currentPage
            ), homePageFragment, object : RxJavaCallBack<AdvertisementBean>() {
                override fun onSuccess(t: AdvertisementBean?) {
                    homePageView?.recommendActivitiesListGetSuccess(t)
                }
            })
    }

    /**
     * 获取首页活动推荐 修改
     */
    override fun getRecommendActivitiesListEx(
        homePageFragment: HomePageFragment,
        currentPage: Int
    ) {
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().getRecommendActivitiesListEx(adpos = 20, curpage = currentPage
            ), homePageFragment,
            object : RxJavaCallBack<AdvertisementBean>() {
                override fun onSuccess(t: AdvertisementBean?) {
                    homePageView?.recommendActivitiesListGetSuccess(t)
                }
            })
    }

    /**
     * 获取首页活动推荐
     */
    override fun getRecommendInfo(homePageFragment: HomePageFragment, infoid: Int) {
        RxJavaUtils.useInSupportFragmentOnDestoryView(
            RetrofitUtils.getApi().getRecommendInfo(infoid),
            homePageFragment,
            object : RxJavaCallBack<HomeRecommendInfoBean>() {
                override fun onSuccess(homeRecommendInfoBean: HomeRecommendInfoBean?) {
                    homePageView?.recommendInfoSuccess(homeRecommendInfoBean)
                }
            })
    }

    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        homePageView = baseView as? HomePageView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        homePageView = null
    }


}